package com.zhousb.deviceManager.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhousb.deviceManager.listener.UploadDataListener;
import com.zhousb.deviceManager.mapper.WxInventoryDetailMapper;
import com.zhousb.deviceManager.mapper.WxInventoryMapper;
import com.zhousb.deviceManager.model.bo.CreateAssetByInventoryBO;
import com.zhousb.deviceManager.model.dto.UploadFileDTO;
import com.zhousb.deviceManager.model.entity.KtWxInventory;
import com.zhousb.deviceManager.model.entity.KtWxInventoryDetail;
import com.zhousb.deviceManager.model.enums.OrderStatusEnum;
import com.zhousb.deviceManager.model.enums.ScanStatusEnum;
import com.zhousb.deviceManager.model.vo.WxOrderVO;
import com.zhousb.deviceManager.service.WxInventoryService;
import com.zhousb.deviceManager.utils.AuthUtils;
import com.zhousb.deviceManager.utils.IdGenerator;
import com.zhousb.deviceManager.utils.OSSUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@Slf4j
public class WxInventoryServiceImpl implements WxInventoryService {

    @Resource
    private WxInventoryDetailMapper wxInventoryDetailMapper;
    @Resource
    private WxInventoryMapper wxInventoryMapper;
    @Resource
    OSSUtil ossUtil;

    @Override
    public Object create(String phone, MultipartFile multipartFile) throws IOException {
        log.info("当前用户手机号：{}", phone);
        LambdaUpdateWrapper<KtWxInventory> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(KtWxInventory::getOperator, phone)
                .in(KtWxInventory::getOrderStatus, Arrays.asList(OrderStatusEnum.CREATED.getCode(), OrderStatusEnum.STARTED.getCode()))
                .set(KtWxInventory::getOrderStatus, OrderStatusEnum.CANCEL.getCode());
        wxInventoryMapper.update(updateWrapper);

        KtWxInventory ktWxInventory = new KtWxInventory();
        ktWxInventory.setId(IdGenerator.nextId());
        ktWxInventory.setOperator(phone);
        ktWxInventory.setOrderCode("WXPD-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        ktWxInventory.setOrderStatus(OrderStatusEnum.CREATED.getCode());
        wxInventoryMapper.insert(ktWxInventory);

        EasyExcel.read(multipartFile.getInputStream(), UploadFileDTO.class, new UploadDataListener(wxInventoryDetailMapper, ktWxInventory.getId())).sheet().doRead();
        return true;
    }

    @Resource
    AuthUtils authUtils;

    @Override
    public Object current() {
        String phone = authUtils.getUser().getUserTel();
        LambdaQueryWrapper<KtWxInventory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KtWxInventory::getOperator, phone)
                .in(KtWxInventory::getOrderStatus, Arrays.asList(OrderStatusEnum.CREATED.getCode(), OrderStatusEnum.STARTED.getCode()));
        KtWxInventory wxInventory = wxInventoryMapper.selectOne(queryWrapper);
        return wxInventory.getId();
    }

    @Override
    public Object list(Integer current,String inventoryId) {
        Page<KtWxInventoryDetail> page = new Page<>();
        page.setCurrent(current);
        page.setSize(20);
        LambdaQueryWrapper<KtWxInventoryDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KtWxInventoryDetail::getInventoryId, inventoryId);
        return wxInventoryDetailMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Object scanned(Integer current,String inventoryId,String keyword) {
        Page<KtWxInventoryDetail> page = new Page<>();
        page.setCurrent(current);
        page.setSize(20);
        LambdaQueryWrapper<KtWxInventoryDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KtWxInventoryDetail::getInventoryId, inventoryId)
                .eq(KtWxInventoryDetail::getStatus, ScanStatusEnum.SCANNED.getCode())
                .like(StringUtils.hasText(keyword),KtWxInventoryDetail::getCode,keyword);
        return wxInventoryDetailMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Object checkCode(String inventoryId, String code) {

        LambdaQueryWrapper<KtWxInventoryDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KtWxInventoryDetail::getInventoryId, inventoryId)
                .eq(KtWxInventoryDetail::getCode, code);
        KtWxInventoryDetail ktWxInventoryDetail = wxInventoryDetailMapper.selectOne(queryWrapper);
        if(ktWxInventoryDetail == null){
            return 0;  //编码不存在
        }else if(ktWxInventoryDetail.getStatus().equals(ScanStatusEnum.SCANNED.getCode())){
            return 2;  //编码已扫描
        }else{
            return 1;  //编码未扫描
        }
    }

    @Override
    public Object submitAsset(CreateAssetByInventoryBO bo) {
        LambdaQueryWrapper<KtWxInventoryDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KtWxInventoryDetail::getInventoryId, bo.getInventoryId())
                .eq(KtWxInventoryDetail::getCode, bo.getCode());
        KtWxInventoryDetail dto = wxInventoryDetailMapper.selectOne(queryWrapper);
        if (Objects.isNull(dto)) {
            dto = new KtWxInventoryDetail();
            BeanUtils.copyProperties(bo, dto);
            dto.setInventoryId(Long.parseLong(bo.getInventoryId()));
            dto.setStatus(ScanStatusEnum.SCANNED.getCode());
            wxInventoryDetailMapper.insert(dto);
            return true;
        } else {
            BeanUtils.copyProperties(bo, dto);
            dto.setInventoryId(Long.parseLong(bo.getInventoryId()));
            dto.setStatus(ScanStatusEnum.SCANNED.getCode());
            wxInventoryDetailMapper.updateById(dto);
            return true;
        }
    }

    @Override
    public Object submitOrder(String inventoryId) {
        LambdaUpdateWrapper<KtWxInventory> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(KtWxInventory::getId, inventoryId)
                .set(KtWxInventory::getOrderStatus, OrderStatusEnum.FINISHED.getCode());
        wxInventoryMapper.update(updateWrapper);
        return true;
    }

    @Override
    public Object reset(String inventoryId) {
        LambdaUpdateWrapper<KtWxInventoryDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(KtWxInventoryDetail::getInventoryId, inventoryId)
                .set(KtWxInventoryDetail::getStatus, ScanStatusEnum.CREATED.getCode());
        wxInventoryDetailMapper.update(updateWrapper);
        return true;
    }

    @Override
    public Object cancel(String inventoryId) {
        LambdaUpdateWrapper<KtWxInventory> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(KtWxInventory::getId, inventoryId)
                .set(KtWxInventory::getOrderStatus, OrderStatusEnum.CANCEL.getCode());
        wxInventoryMapper.update(updateWrapper);
        return true;
    }

    @Override
    public Object uploadImage(MultipartFile file) {
        return ossUtil.upload(file);
    }

    @Override
    public Object export() {
        return null;
    }
}
